package tio.study.showcase.client;

import org.tio.client.ClientChannelContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.client.TioClientConfig;
import org.tio.client.intf.TioClientHandler;
import org.tio.client.intf.TioClientListener;
import org.tio.core.Node;
import org.tio.core.Tio;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.json.Json;
import tio.study.showcase.common.Const;
import tio.study.showcase.common.ShowcasePacket;
import tio.study.showcase.common.Type;
import tio.study.showcase.common.packets.GroupMsgReqBody;
import tio.study.showcase.common.packets.JoinGroupReqBody;
import tio.study.showcase.common.packets.LoginReqBody;
import tio.study.showcase.common.packets.P2PReqBody;

import java.util.Scanner;

/**
 * @description:
 * @author: zhou shuai
 * @date: 2022/12/2 14:18
 * @version: v1
 */
public class ShowcaseClientStarter {

    static String serverIp = "127.0.0.1";
    static int serverPort = Const.PORT;

    private static Node serverNode = new Node(serverIp, serverPort);

    /**
     * 用来自动连接的，不想自动连接请设为null
     */
    //private static ReconnConf reconnConf = new ReconnConf(5000L);
    private static ReconnConf reconnConf = null;

    private static TioClientHandler tioClientHandler = new ShowcaseClientTioHandler();

    private static TioClientListener tioClientListener = new ShowcaseClientTioListener();
    /**
     * 一组连接共用的上下文对象
     */
    public static TioClientConfig tioClientConfig = new TioClientConfig(tioClientHandler, tioClientListener, reconnConf);

    private static TioClient tioClient = null;

    static ClientChannelContext clientChannelContext;

    public static void main(String[] args) throws Exception {
        tioClient = new TioClient(tioClientConfig);
        clientChannelContext = tioClient.connect(serverNode);
        command();
    }

    public static void command() throws Exception {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        int i = 1;
        StringBuilder sb = new StringBuilder();
        sb.append("使用指南:\r\n");
        sb.append(i++ + "、需要帮助，输入 '?'.\r\n");
        sb.append(i++ + "、登录，输入 'login loginname password'.\r\n");
        sb.append(i++ + "、进入群组，输入 'join group1'.\r\n");
        sb.append(i++ + "、群聊，输入 'groupMsg group1 text'.\r\n");
        sb.append(i++ + "、点对点聊天，输入 'p2pMsg loginname text'.\r\n");

        sb.append(i++ + "、退出程序，输入 'exit'.\r\n");

        System.out.println(sb);
        // 这个就是用户输入的数据
        String line = sc.nextLine();
        while (true) {
            if ("exit".equalsIgnoreCase(line)) {
                System.out.println("Thanks for using! bye bye.");
                break;
            } else if ("?".equals(line)) {
                System.out.println(sb);
            }

            processCommand(line);
            // 这个就是用户输入的数据
            line = sc.nextLine();
        }

        tioClient.stop();
        System.exit(0);
    }

    public static void processCommand(String line) throws Exception {
        if (StrUtil.isEmpty(line)) {
            return;
        }

        String[] args = StrUtil.split(line," ");
        String command = args[0];

        if ("login".equalsIgnoreCase(command)) {
            String loginname = args[1];
            String password = args[2];

            LoginReqBody loginReqBody = new LoginReqBody();
            loginReqBody.setLoginname(loginname);
            loginReqBody.setPassword(password);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.LOGIN_REQ);
            reqPacket.setBody(Json.toJson(loginReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);

        } else if ("join".equals(command)) {
            String group = args[1];

            JoinGroupReqBody joinGroupReqBody = new JoinGroupReqBody();
            joinGroupReqBody.setGroup(group);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.JOIN_GROUP_REQ);
            reqPacket.setBody(Json.toJson(joinGroupReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
        } else if ("groupMsg".equals(command)) {
            String group = args[1];
            String text = args[2];

            GroupMsgReqBody groupMsgReqBody = new GroupMsgReqBody();
            groupMsgReqBody.setToGroup(group);
            groupMsgReqBody.setText(text);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.GROUP_MSG_REQ);
            reqPacket.setBody(Json.toJson(groupMsgReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
        } else if ("p2pMsg".equals(command)) {
            String toUserid = args[1];
            String text = args[2];

            P2PReqBody p2pReqBody = new P2PReqBody();
            p2pReqBody.setToUserid(toUserid);
            p2pReqBody.setText(text);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.P2P_REQ);
            reqPacket.setBody(Json.toJson(p2pReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
        }

    }

}
